package com.zwb.aiwebsite.service.impl;

import com.zwb.aiwebsite.dto.SolutionDTO;
import com.zwb.aiwebsite.entity.solution.TbSolution;
import com.zwb.aiwebsite.entity.solution.TbSolutionInfo;
import com.zwb.aiwebsite.entity.solution.TbSolutionType;
import com.zwb.aiwebsite.repository.TbSolutionInfoRepository;
import com.zwb.aiwebsite.repository.TbSolutionRepository;
import com.zwb.aiwebsite.repository.TbSolutionTypeRepository;
import com.zwb.aiwebsite.service.TbSolutionService;
import com.zwb.aiwebsite.utils.MyStringUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @author 2825075112@qq.com
 */
@Service
@AllArgsConstructor
public class TbSolutionServiceImpl implements TbSolutionService {


    private final TbSolutionTypeRepository tbSolutionTypeRepository;
    private final TbSolutionInfoRepository tbSolutionInfoRepository;
    private final TbSolutionRepository tbSolutionRepository;

    @Override
    public void addSolutionType(TbSolutionType tbSolutionType) {
        if (StringUtils.isBlank(tbSolutionType.getName())){
            return;
        }
        if (tbSolutionType.getOrder() == null){
            tbSolutionType.setOrder(1);
        }
        tbSolutionTypeRepository.save(tbSolutionType);
    }

    @Override
    public List<TbSolutionType> getAllSolutionType() {
        return tbSolutionTypeRepository.findAll();
    }

    @Override
    public void delSolutionType(Integer id) {
        tbSolutionTypeRepository.deleteById(id);
    }

    @Override
    public List<TbSolution> getAllSolution() {
        return tbSolutionRepository.findAll();
    }

    @Override
    public void delSolution(Integer id) {
        tbSolutionRepository.deleteById(id);
        tbSolutionInfoRepository.deleteById(id);
    }

    @Override
    public void addSolution(SolutionDTO solutionDTO) {
        if (StringUtils.isBlank(solutionDTO.getTitle())){
            return;
        }
        if (StringUtils.isBlank(solutionDTO.getImgUrl())){
            return;
        }
        if (StringUtils.isBlank(solutionDTO.getContent())){
            return;
        }
        if (solutionDTO.getOrder() == null){
            solutionDTO.setOrder(1);
        }
        TbSolution tbSolution = new TbSolution();
        TbSolutionInfo tbSolutionInfo = new TbSolutionInfo();
        BeanUtils.copyProperties(solutionDTO, tbSolution);
        BeanUtils.copyProperties(solutionDTO, tbSolutionInfo);
        tbSolution.setContentIntroduction(MyStringUtils.getWordCount(tbSolution.getContentIntroduction()));
        tbSolutionRepository.save(tbSolution);
        tbSolutionInfoRepository.save(tbSolutionInfo);
    }

    @Override
    public void SolutionMenu(Integer id, Model model) {
        Optional<TbSolutionType> tbProductTypeOptional = tbSolutionTypeRepository.findById(id);
        if (!tbProductTypeOptional.isPresent()){
            return;
        }
        TbSolutionType tbSolutionType = tbProductTypeOptional.get();
        model.addAttribute("solutionTypeName", tbSolutionType.getName());
        List<TbSolution> all = tbSolutionRepository.findAllBySolutionType(id);
        all.sort(Comparator.comparing(TbSolution::getOrder));
        model.addAttribute("solutionList", all);
    }

    @Override
    public List<SolutionDTO> getAllSolutionInfo() {
        List<TbSolution> all = tbSolutionRepository.findAll();
        List<SolutionDTO> result = new ArrayList<>();
        for (TbSolution TbSolution : all) {
            SolutionDTO solutionDTO = new SolutionDTO();
            BeanUtils.copyProperties(TbSolution, solutionDTO);
            Optional<TbSolutionInfo> tbProductInfoOptional = tbSolutionInfoRepository.findById(TbSolution.getId());
            if (tbProductInfoOptional.isPresent()) {
                TbSolutionInfo TbSolutionInfo = tbProductInfoOptional.get();
                BeanUtils.copyProperties(TbSolutionInfo, solutionDTO);
            }
            result.add(solutionDTO);
        }
        return result;
    }

    @Override
    public void SolutionInfoPage(Model model, Integer id) {
        Optional<TbSolutionInfo> tbProductInfoOptional = tbSolutionInfoRepository.findById(id);
        if (tbProductInfoOptional.isPresent()) {
            TbSolutionInfo TbSolutionInfo = tbProductInfoOptional.get();
            model.addAttribute("tbSolutionInfo", TbSolutionInfo);
        }
        Optional<TbSolution> tbSolutionOptional = tbSolutionRepository.findById(id);
        if (tbSolutionOptional.isPresent()) {
            TbSolution tbSolution = tbSolutionOptional.get();
            model.addAttribute("tbSolution", tbSolution);
        }
        Optional<TbSolutionType> tbSolutionTypeOptional = tbSolutionTypeRepository.findById(id);
        if (tbSolutionTypeOptional.isPresent()) {
            TbSolutionType tbSolutionType = tbSolutionTypeOptional.get();
            model.addAttribute("tbSolutionType", tbSolutionType);
        }
    }
}
